package 地区数据_分省年度数据;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

import java.io.File;
import java.io.FileOutputStream;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 
 * 公司：上海经禾
 * 作者：薛业成
 * 功能：导出为excel表格工具类
 * 时间：2016年11月11日下午3:08:26
 *
 */
public class WriteExcel {
	public static XSSFWorkbook workbook=new XSSFWorkbook();
	public static XSSFSheet sheet=workbook.createSheet();
	//Excel表格内容部分
	public static void writeExcel(Object obj, int rowNumber, String[] chineseNames){
		List<String> list=new ArrayList<String>();
		Class classObj=obj.getClass();
		Field[] field=classObj.getDeclaredFields();
		String[] names=new String[field.length];
		String mothedGetName=null;
		Map<String, String> map=new HashMap<String, String>();
		for(int j=0;j<field.length;j++){
			names[j]=field[j].getName();
			String s1=names[j].substring(0,1);
			String s2=names[j].substring(1);
			s1=s1.toUpperCase();
			names[j]=s1+s2;
			mothedGetName="get"+names[j];
			map.put(chineseNames[j],mothedGetName);
		}
		for(int n=0;n<map.size();n++){
			try {
				Method methodGet=classObj.getMethod(map.get(chineseNames[n]));
				String s=methodGet.invoke(obj)+"";
				if(s.equals("null") || s==null || s==""){
					list.add("--");
					System.out.println("--");
				}else{
					list.add(s);
					System.out.println(s);
				}
			} catch (NoSuchMethodException e) {
				e.printStackTrace();
			} catch (SecurityException e) {
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				e.printStackTrace();
			} catch (IllegalArgumentException e) {
				e.printStackTrace();
			} catch (InvocationTargetException e) {
				e.printStackTrace();
			}
		}
		XSSFRow rows=sheet.createRow(rowNumber);
		for(int j=0;j<chineseNames.length;j++){
			rows.createCell(j).setCellValue(list.get(j));
		}
		System.out.println("该条数据获取完毕！");
		System.out.println();
	}
	
		
	//Excel表格内容部分(方法重载)
		public static void writeExcel(Object obj, int rowNumber, String[] chineseNames, List<List<String>> valueList){
			List<String> list=new ArrayList<String>();
			Class classObj=obj.getClass();
			Field[] field=classObj.getDeclaredFields();
			String[] names=new String[field.length];
			String mothedGetName=null;
			Map<String, String> map=new HashMap<String, String>();
			for(int j=0;j<field.length;j++){
				names[j]=field[j].getName();
				String s1=names[j].substring(0,1);
				String s2=names[j].substring(1);
				s1=s1.toUpperCase();
				names[j]=s1+s2;
				mothedGetName="get"+names[j];
				map.put(chineseNames[j],mothedGetName);
			}
			for(int n=0;n<map.size();n++){
				try {
					Method methodGet=classObj.getMethod(map.get(chineseNames[n]));
					String s=methodGet.invoke(obj)+"";
					if(s.equals("null") || s==null || s==""){
						list.add("--");
						System.out.println("--");
					}else{
						list.add(s);
						System.out.println(s);
					}
				} catch (NoSuchMethodException e) {
					e.printStackTrace();
				} catch (SecurityException e) {
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					e.printStackTrace();
				} catch (IllegalArgumentException e) {
					e.printStackTrace();
				} catch (InvocationTargetException e) {
					e.printStackTrace();
				}
			}
			XSSFRow rows=sheet.createRow(rowNumber);
			for(int j=0;j<chineseNames.length;j++){
				rows.createCell(j).setCellValue(list.get(j));
			}
			for(int i=0;i<valueList.size();i++){
				rows.createCell(chineseNames.length+i).setCellValue(valueList.get(i)+"");
			}
			System.out.println("该文件获取完毕！");
			System.out.println();
		}
	//Excel表格内容部分(方法重载)
	public static void writeExcel(int rowNumber, List<String> valueList, XSSFSheet sheet, List<String> memolist){
		XSSFRow rows=sheet.createRow(rowNumber);
		for(int i=0;i<valueList.size();i++){
			rows.createCell(i).setCellValue(valueList.get(i)+"");
		}
		if(memolist.size()>0) {
			int maxrow = sheet.getLastRowNum() + 1;
			XSSFRow rows1 = sheet.createRow(maxrow);
			String memo="注：";
			for(int j=0; j<memolist.size();j++ ){
                  memo = memo+(j+1)+". "+memolist.get(j).replace("[","").replace("]","").replace(",","").replace("|","")+" ";
			}
			rows1.createCell(0).setCellValue(memo);
		}
		System.out.println("该文件获取完毕！");
		System.out.println();
	}
	public static void writeExcel(int rowNumber, List<String> valueList, XSSFSheet sheet){
		XSSFRow rows=sheet.createRow(rowNumber);
		for(int i=0;i<valueList.size();i++){
			rows.createCell(i).setCellValue(valueList.get(i)+"");
		}
		System.out.println("该文件获取完毕！");
		System.out.println();
	}
	
	//Excel表格标题部分
	public static void writeHead(String[] chineseNames, XSSFSheet sheet){
		XSSFRow row=sheet.createRow(0);
		for(int i=0;i<chineseNames.length;i++){
			row.createCell(i).setCellValue(chineseNames[i]);
			sheet.setColumnWidth(i,5000);
		}
	}
	
	//导出为excel文件
	public static void exportExcel(String loacl, String excelName, XSSFWorkbook workbook){
		try {
			File file = new File(loacl + excelName + ".xlsx");
			FileOutputStream fileOut = new FileOutputStream(file);
			workbook.write(fileOut);
			System.out.println("导出成功！");
			fileOut.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
}
